package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * ClassName: StringToItemTypeConverter
 * Package: com.atguigu.lease.web.admin.custom.converter
 *
 * @Create 2025/10/29 18:12
 * @Author 阿瓒
 * @Version 1.0
 * Description:
 */
@Component
public class StringToItemTypeConverter implements Converter<String , ItemType> {


    /**
     * 类型转化
     * @param source 源数据
     * @return ItemType
     */
    @Override
    public ItemType convert(String source) {
        ItemType[] itemTypes = ItemType.values();
        for (ItemType itemType : itemTypes) {
            if(source.equals(itemType.getCode()+"")){
                return itemType;
            }
        }

//        if(source.equals("1")){
//            return ItemType.APARTMENT;
//        }else if(source.equals("2")){
//            return ItemType.ROOM;
//        }
        //主动抛出异常
        throw new RuntimeException("传入参数类型错误!必须是1或者2!!");
    }
}
